package com.linkhub.oms.dao.mpservice.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.linkhub.oms.common.enums.mp.LocalProductTypeEnum;
import com.linkhub.oms.dao.mapper.LocalProductSupplierEntityMapper;
import com.linkhub.oms.dao.mpservice.ILocalProductSupplierService;
import com.linkhub.oms.entity.LocalProductEntity;
import com.linkhub.oms.entity.LocalProductSupplierEntity;
import com.linkhub.oms.entity.OrderProductEntity;
import com.linkhub.oms.entity.SupplierEntity;
import com.linkhub.oms.entity.resulttype.entryorder.EntryOrderLineResultEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 本地商品供应商表 服务实现类
 * </p>
 *
 * @author echo
 * @since 2024-05-04
 */
@Service
public class LocalProductSupplierServiceImpl extends ServiceImpl<LocalProductSupplierEntityMapper, LocalProductSupplierEntity> implements ILocalProductSupplierService {

    @Resource
    private LocalProductSupplierEntityMapper localProductSupplierEntityMapper;


    @Override
    public List<EntryOrderLineResultEntity> queryUserSupplierSkuList( String userId,
                                                                      String supplierId,
                                                                      Collection<String> skus ) {
        MPJLambdaWrapper<LocalProductSupplierEntity> wrapper = JoinWrappers.lambda(LocalProductSupplierEntity.class);

        wrapper.selectAs(LocalProductSupplierEntity::getSysSupplierId, EntryOrderLineResultEntity::getSysSupplierId)
                .selectAs(LocalProductSupplierEntity::getSkuId, EntryOrderLineResultEntity::getSkuId)
                .selectAs(LocalProductEntity::getSku, EntryOrderLineResultEntity::getSku)
                .selectAs(LocalProductEntity::getProductEnTitle, EntryOrderLineResultEntity::getProductEnTitle)

                .innerJoin(LocalProductEntity.class, on ->
                        on.eq(LocalProductEntity::getSkuId, LocalProductSupplierEntity::getSkuId))
                .eq(LocalProductSupplierEntity::getUserId, userId)
                .eq(LocalProductSupplierEntity::getSysSupplierId, supplierId)
                .in(LocalProductEntity::getSku, skus);

        return localProductSupplierEntityMapper.selectJoinList(EntryOrderLineResultEntity.class, wrapper);
    }

    @Override
    public List<LocalProductEntity> queryLocalProductsBySupplierId( String supplierId, Boolean isRecordStock ) {

        MPJLambdaWrapper<LocalProductSupplierEntity> wrapper = JoinWrappers.lambda(LocalProductSupplierEntity.class);

        wrapper
                .select(LocalProductEntity::getSkuId)
                .select(LocalProductEntity::getSku)
                .select(LocalProductEntity::getProductEnTitle)
                .select(LocalProductEntity::getSpu)
                .select(LocalProductEntity::getImgUrl)
                .select(LocalProductEntity::getProductType)

                .innerJoin(LocalProductEntity.class, on ->
                        on.eq(LocalProductEntity::getSkuId, LocalProductSupplierEntity::getSkuId))
                .eq(LocalProductSupplierEntity::getSysSupplierId, supplierId)

                .and(Boolean.TRUE.equals(isRecordStock), w ->
                        w.in(LocalProductEntity::getProductType, LocalProductTypeEnum.SINGLE_PRODUCT.getCode(), LocalProductTypeEnum.PACKAGING_MATERIAL.getCode())
                                .eq(LocalProductEntity::getIsRecordStock, 1));

        return localProductSupplierEntityMapper.selectJoinList(LocalProductEntity.class, wrapper);
    }

    @Override
    public List<LocalProductSupplierEntity> querySysSupplierIdBySkuId( String userId, String skuId ) {
        return lambdaQuery()
                .eq(LocalProductSupplierEntity::getUserId, userId)
                .eq(LocalProductSupplierEntity::getSkuId, skuId)
                .list();
    }

    @Override
    public void removeByUserIdAndSkuId( String merchantUserId, String skuId ) {
        lambdaUpdate()
                .eq(LocalProductSupplierEntity::getUserId, merchantUserId)
                .eq(LocalProductSupplierEntity::getSkuId, skuId)
                .remove();
    }
}
